package lc;
import java.util.concurrent.atomic.*;
public class Ex1115 {
    class FooBar {
        private int n;
        private AtomicBoolean a, b;
    
        public FooBar(int n) {
            this.n = n;
            a = new AtomicBoolean(false);
            b = new AtomicBoolean(true);
        }
    
    
        public void foo(Runnable printFoo) throws InterruptedException {
            
            for (int i = 0; i < n; i++) {
                while (a.get()) {
                    //阻塞
                }
                // printFoo.run() outputs "foo". Do not change or remove this line.
                printFoo.run();
                a.set(true);
                b.set(false);
            }
        }
    
        public void bar(Runnable printBar) throws InterruptedException {
            
            for (int i = 0; i < n; i++) {
                while (b.get()) {
    
                }
                // printBar.run() outputs "bar". Do not change or remove this line.
                printBar.run();
                a.set(false);
                b.set(true);
            }
        }
    }
}
